﻿// -----------------------------------------------------------------------
// <copyright file="GameBoard.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace B13_Ex02
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class GameBoard
    {
        private const int k_MinBoardSize = 6;
        private const int k_MidBoardSize = 8;
        private const int k_MaxBoardSize = 10;
        private readonly int m_BoardSize;
        private readonly BoardSquare[,] m_GameBoard;

        public BoardSquare[,] GameBoardCurrent
        {
            get { return m_GameBoard; }
        }

        public GameBoard()
            : this(k_MinBoardSize)
        {
        }

        public void ResetGameBoard()
        {
            for (int i = 0; i < m_BoardSize; i++)
            {
                for (int j = 0; j < m_BoardSize; j++)
                {
                    m_GameBoard[i, j] = null;
                }
            }
        }

        public GameBoard(int i_BoardSize)
        {
            m_BoardSize = i_BoardSize;
            m_GameBoard = new BoardSquare[m_BoardSize, m_BoardSize];
            for (int i = 0; i < m_BoardSize; i++)
            {
                for (int j = 0; j < m_BoardSize; j++)
                {
                    m_GameBoard[i, j] = new BoardSquare();
                }
            }
        }

        public int MinBoardSize
        {
            get { return k_MinBoardSize; }
        }

        public int MidBoardSize
        {
            get { return k_MidBoardSize; }
        }

        public int MaxBoardSize
        {
            get { return k_MaxBoardSize; }
        }

        public BoardSquare[,] Board
        {
            get { return m_GameBoard; }
        }

        public int Size
        {
            get { return m_BoardSize; }
        }

        public void RemovePiece(int i_Xpostion, int i_Yposition)
        {
            m_GameBoard[i_Xpostion, i_Yposition].SquareState = ' ';
        }

        public void InitializeBoard(PlayerInfo i_PlayerOne, PlayerInfo i_PlayerTwo)
        {
            int boardFirstHalf = (m_BoardSize / 2) - 1;
            int boardSecondHalf = (m_BoardSize / 2) + 1;

            for (int i = 0; i < m_BoardSize; i++)
            {
                for (int j = 0; j < m_BoardSize; j++)
                {
                    if (((i + j) % 2) == 0)
                    {
                        m_GameBoard[i, j].SquareState = m_GameBoard[i, j].Empty;
                        m_GameBoard[i, j].IsValidSquare = false;
                    }
                    else
                    {
                        if (i < boardFirstHalf)
                        {
                            m_GameBoard[i, j].SquareState = i_PlayerOne.Color;
                        }
                        else if (i >= boardSecondHalf)
                        {
                            m_GameBoard[i, j].SquareState = i_PlayerTwo.Color;
                        }
                        else
                        {
                            m_GameBoard[i, j].SquareState = m_GameBoard[i, j].Empty;
                        }
                        
                        m_GameBoard[i, j].IsValidSquare = true;
                    }
                }
            }
        }
    }
}